//
//  CTCResponderDetailVC.m
//  ConsultantVersion
//
//  Created by 李小龙 on 2017/3/27.
//  Copyright © 2017年 jieshuibao. All rights reserved.
//

#import "CTCResponderDetailVC.h"
#import "CTCResponderQuestionCell.h"
#import "CTCAnswerQuestionVC.h"

@interface CTCResponderDetailVC ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIButton *answerBtn;
/** 答案的uuid */
@property (nonatomic, copy) NSString *uuid;

@end

@implementation CTCResponderDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setupUI];
    
    [self setupTableView];
    
//    [self getQustionDeatilData];
    

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CTCResponderQuestionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CTCResponderQuestionCell"];
    cell.questionModel = self.questionModel;
    return cell;
}


- (void)setupTableView
{
    self.tableView.estimatedRowHeight = 200;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.sectionHeaderHeight = 10;
    self.tableView.sectionFooterHeight = 0;
    self.tableView.contentInset = UIEdgeInsetsMake(11-35, 0, 0, 0);
}



- (IBAction)answerBtnClick {
    // UIAlertViewController(弹框视图)
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"您确认抢答吗，如果抢答成功一定要认真作答，否则会影响您的信用。" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        [self.navigationController popViewControllerAnimated:YES];
    }];
    [alert addAction:cancel];
    
    UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        if (self.fromType == ResponderDetailFromTypeRapid) {
            [self answerQusetionRequest];
        }
        
        else {
            [self jumpToAnswerPageUuid:self.questionModel.uuid];
        }
        
    }];
    [alert addAction:confirm];
    
    
    [self presentViewController:alert animated:YES completion:nil];

   
}

- (void)answerQusetionRequest
{
    [MBProgressHUD showAnimationHUD];
    [RequestTool POST:kGrabRespondeUrl
           parameters:@{@"uuid":self.questionModel.uuid}
             progress:nil
              success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                  DeLog(@"responseObject %@", responseObject);
        
                  NSInteger code = [responseObject[@"code"] integerValue];
                  NSString *des = responseObject[@"des"];
                  if (code == 1) {
                      [MBProgressHUD showSuccessMessage:des];
                      if (self.responderSuccessBlock) {
                          self.responderSuccessBlock();
                      }

                      [self jumpToAnswerPageUuid:responseObject[@"data"]];
                      
                  }
                  else {
                      [MBProgressHUD showErrorMessage:des];
                      
                      if (code == 1) {
                          CTCLoginViewController *vc = [CTCLoginViewController viewControllerFromStoryboard];
                          MPWeakSelf(self);
                          vc.loginSuccess = ^(){
                              [weakself answerQusetionRequest];
                          };
                          [self presentViewController:vc animated:YES completion:nil];
                      }
                    }
              } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                  DeLog(@"error %@", error);
                  if ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable) {
                      [MBProgressHUD showErrorMessage:@"网络不给力"];
                  }
                  else {
                      [MBProgressHUD showErrorMessage:@"服务器繁忙"];
                  }
              }];
}

- (void)setupUI
{
    self.title = @"咨询详情";
    
    if (([self.questionModel.status integerValue] == 1)
        ||([self.questionModel.status integerValue] == 4)) {
        self.answerBtn.enabled = NO;
        self.answerBtn.backgroundColor = HEXCOLOR(0x999999);
    }
    else if ([self.questionModel.status integerValue] == 0) {
        self.answerBtn.enabled = YES;
        self.answerBtn.backgroundColor = HEXCOLOR(0xFA6A31);

    }
}


- (void)jumpToAnswerPageUuid:(NSString *)uuid
{
    UIStoryboard *story = [UIStoryboard storyboardWithName:@"CTCResponderVC" bundle:nil];
    CTCAnswerQuestionVC *vc = [story instantiateViewControllerWithIdentifier:@"CTCAnswerQuestionVC"];
    self.questionModel.uuid = uuid;
    vc.questionModel = self.questionModel;
    [self.navigationController pushViewController:vc animated:YES];
}

@end
